package com.lecampus.businessCampus_moments.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lecampus.businessCampus_moments.entity.Article;
import com.lecampus.businessCampus_moments.entity.ArticleComment;
import com.lecampus.businessCampus_moments.mapper.ArticleCommentMapper;
import com.lecampus.businessCampus_moments.mapper.ArticleMapper;
import com.lecampus.businessCampus_moments.mapper.UserMapper;
import com.lecampus.businessCampus_moments.service.ArticleCommentService;
import com.lecampus.businessCampus_moments.vo.ArticleCommentVO;
import com.lecampus.businessCampus_moments.vo.UserVO;
import com.lecampus.commons.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-14
 */
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleComment> implements ArticleCommentService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleCommentMapper articleCommentMapper;
    @Autowired
    private UserMapper userMapper;

    private User user;
    private UserVO userVO;
    private ArticleCommentVO articleCommentVO;
    private List<ArticleComment> articleCommentList;
    private List<ArticleCommentVO> articleCommentVOList;

    private QueryWrapper<ArticleComment> articleCommentQueryWrapper;

    /**
     * 根据文章id查询评论
     */
    @Override
    public List<ArticleCommentVO> findCommentsByArticleId(String articleId,long currentPage,long size) {
        //分页
        Page<ArticleComment> page = new Page<>(currentPage,size);
        //条件
        articleCommentQueryWrapper = new QueryWrapper<>();
        articleCommentQueryWrapper.eq("article_id",articleId);

        articleCommentMapper.selectPage(page,articleCommentQueryWrapper);
        articleCommentList = page.getRecords();

        articleCommentVOList = articleCommentVOListUtil(articleCommentList);

        return articleCommentVOList;
    }

    /**
     * 添加评论
     */
    @Override
    public void inputComment(String articleId,ArticleComment comment) {
        //添加评论
        articleCommentMapper.insert(comment);

        //增加评论数
        Article article = articleMapper.selectById(articleId);
        article.setCommentSum(article.getCommentSum() + 1);
    }

    /**
     * 删除评论
     */
    @Override
    public void deleteByCommentId(String articleId,String commentId) {
        //减少评论数
        Article article = articleMapper.selectById(articleId);
        article.setCommentSum(article.getCommentSum() - 1);

        //删除评论
        articleCommentMapper.deleteById(commentId);
    }

    /**
     * 封装评论
     */
    private ArticleCommentVO articleCommentVOUtil(ArticleComment articleComment) {
        user = userMapper.selectById(articleComment.getUserId());
        System.out.println("user"+user);
        userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);

        articleCommentVO = new ArticleCommentVO();
        articleCommentVO.setUserVO(userVO);
        BeanUtils.copyProperties(articleComment,articleCommentVO);

        return articleCommentVO;
    }

    /**
     * 封装评论列表
     */
    private List<ArticleCommentVO> articleCommentVOListUtil(List<ArticleComment> articleCommentList) {
        articleCommentVOList = new ArrayList<>();
        for (ArticleComment articleComment : articleCommentList) {
            articleCommentVOUtil(articleComment);
            articleCommentVOList.add(articleCommentVO);
        }
        return articleCommentVOList;
    }

    /**
     * 点赞
     */
    @Override
    public void likes(String articleCommentId,int flag) {
        ArticleComment articleComment = articleCommentMapper.selectById(articleCommentId);

        //点赞
        if (flag == 0) {
            articleComment.setLikeNum(articleComment.getLikeNum() + 1);
        } else if (flag == 1 && articleComment.getLikeNum() > 0) {
            //取消点赞
            articleComment.setLikeNum(articleComment.getLikeNum() - 1);
        }
        articleCommentMapper.updateById(articleComment);
    }
}
